
package battleroom;

import com.jme3.bounding.BoundingBox;
import com.jme3.collision.CollisionResult;
import com.jme3.collision.CollisionResults;
import com.jme3.input.MouseInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.MouseButtonTrigger;
import com.jme3.material.Material;
import com.jme3.material.RenderState.FaceCullMode;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Ray;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Spatial;
import com.jme3.scene.shape.Box;
import com.jme3.scene.shape.Line;

/**
 *
 * @author AdamZ and Zach
 */
public class BaseGun 
{

    private Spatial gun;
    private Main app;
    
    public BaseGun (Main app)
    {
        this.app=app;
        Box box1 = new Box(1f,1f,5f);    //Size of the gun    
        gun = new Geometry("star", box1 );

        Material mat1 = new Material(app.getAssetManager(), 
                                        "Common/MatDefs/Misc/Unshaded.j3md");
        mat1.setColor("Color", ColorRGBA.Gray);
        gun.setMaterial(mat1); 
        this.getSpatial().setLocalTranslation(app.getCamera().getDirection().x-.5f,
                                              app.getCamera().getDirection().y,
                                              app.getCamera().getDirection().z-5f);
        
        app.getInputManager().addMapping("lmb", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
        app.getInputManager().addListener(actionListener, new String[]{"lmb"});  
        
        
        
        
    }
    
    private ActionListener actionListener = new ActionListener() {
        public void onAction(String name, boolean keyPressed, float tpf) {
          if (name.equals("lmb") && !keyPressed){ 
              fire();
          }
        }        
    };
    
    public Spatial getSpatial(){return gun;}
    
    public void updateGunRotation()
    {    this.getSpatial().setLocalRotation(app.getCamera().getRotation()); }
    
    public void updateGunPosition()
    {
        this.getSpatial().move(app.getCamera().getDirection().x+.5f,
                                                app.getCamera().getDirection().y,
                                                app.getCamera().getDirection().z+1f);
    }
    
    //Fire takes a ray that was cast directly into the screen and returns the collision result
    public CollisionResult fire()
    {
        Ray bulletRay = new Ray(app.getCamera().getLocation(), app.getCamera().getDirection());
        
        Vector3f fireTo;
        CollisionResults results = new CollisionResults();
        CollisionResult hit = null;
        
        app.getStateManager().getState(LevelState.class).getRootNode().collideWith(bulletRay, results);        
        
        if(results.size() > 0)
        {
            hit = results.getClosestCollision();
            
            TargetControl target = hit.getGeometry().getControl(TargetControl.class);
            if(target != null)
            {
                target.onHit();
            }          
            
            fireTo = hit.getContactPoint();
        }else{
            fireTo = getBulletLocation().add(app.getCamera().getDirection().mult(1000));
        }
        
        //Create the bullet
        Material bulletMaterial = new Material(app.getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md");
        //bulletMaterial.getAdditionalRenderState().setFaceCullMode(FaceCullMode.Off);
        Line bulletMesh = new Line(getBulletLocation(), fireTo);
        Geometry bullet = new Geometry("bullet", bulletMesh);
        bullet.setMaterial(bulletMaterial);
        
        BaseBullet bulletControl = new BaseBullet(.1f, getBulletLocation().subtract(gun.getLocalTranslation()), fireTo);
        bullet.addControl(bulletControl);
        
        app.getRootNode().attachChild(bullet);
        
        return hit;
    }

    Vector3f getBulletLocation() {
        Geometry geom = (Geometry)getSpatial();
        BoundingBox box = (BoundingBox)geom.getModelBound();
        float length = box.getZExtent();
        return getSpatial().getParent().getLocalTranslation().add(geom.getLocalTranslation().add(app.getCamera().getDirection().mult(length)));
    }
    
    
}
